package com.heima.homeservicemanagementsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.homeservicemanagementsystem.mapper.ShoppingCartMapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import com.heima.homeservicemanagementsystem.module.PageParams;
import com.heima.homeservicemanagementsystem.module.Result;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import com.heima.homeservicemanagementsystem.entity.ShoppingCart;
import com.heima.homeservicemanagementsystem.service.ShoppingCartService;

import java.util.List;

import static cn.dev33.satoken.SaManager.log;

/**
 * 购物车;(shopping_cart)表控制层
 *
 * @author : http://www.chiner.pro
 * @date : 2025-4-17
 */
@Slf4j
@Tag(name = "购物车对象功能接口")
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;
    @Resource
    private ShoppingCartMapper shoppingCartMapper;

    @Operation(summary = "分页查询")
    @PostMapping("/page")
    public Result<Page<ShoppingCart>> paginQuery(@RequestBody PageParams<ShoppingCart> pageParams) {
        //查询参数
        ShoppingCart params = pageParams.getParams();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();


        lqw.eq(params.getUserId() != null, ShoppingCart::getUserId, params.getUserId());


//        lqw.eq(params.getServiceId() != null, ShoppingCart::getServiceId, params.getServiceId());


//        lqw.eq(params.getImg() != null, ShoppingCart::getImg, params.getImg());
//
//
//        lqw.eq(params.getName() != null, ShoppingCart::getName, params.getName());
//
//
//        lqw.eq(params.getNumber() != null, ShoppingCart::getNumber, params.getNumber());
//
//
//        lqw.eq(params.getPrice() != null, ShoppingCart::getPrice, params.getPrice());
//
//
//        lqw.eq(params.getSpec() != null, ShoppingCart::getSpec, params.getSpec());
//
//
//        lqw.eq(params.getTime() != null, ShoppingCart::getTime, params.getTime());

        return Result.success(shoppingCartService.page(pageParams.getPage(), lqw));
    }


    @Operation(summary = "购物车所有搜索")
    @GetMapping("/list")
    public Result searchQuery() {
        return Result.success(shoppingCartService.list());
    }

    @Operation(summary = "有条件的搜索")
    @PostMapping("/tjSearch")
    public Result Query(@RequestBody ShoppingCart shoppingCart) {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();


        lqw.eq(shoppingCart.getId() != null, ShoppingCart::getId, shoppingCart.getId());


        lqw.eq(shoppingCart.getUserId() != null, ShoppingCart::getUserId, shoppingCart.getUserId());


        lqw.eq(shoppingCart.getServiceId() != null, ShoppingCart::getServiceId, shoppingCart.getServiceId());


        lqw.eq(shoppingCart.getImg() != null, ShoppingCart::getImg, shoppingCart.getImg());


        lqw.like(shoppingCart.getName() != null, ShoppingCart::getName, shoppingCart.getName());


        lqw.eq(shoppingCart.getNumber() != null, ShoppingCart::getNumber, shoppingCart.getNumber());


        lqw.eq(shoppingCart.getPrice() != null, ShoppingCart::getPrice, shoppingCart.getPrice());


        lqw.eq(shoppingCart.getSpec() != null, ShoppingCart::getSpec, shoppingCart.getSpec());


        lqw.eq(shoppingCart.getTime() != null, ShoppingCart::getTime, shoppingCart.getTime());


        return Result.success(shoppingCartService.list(lqw));
    }

    /**
     * 新增数据
     *
     * @param shoppingCart 实例对象
     * @return 实例对象
     */
    @Operation(summary = "新增数据")
    @PostMapping("/insert")
    public Result<Boolean> insert(@RequestBody ShoppingCart shoppingCart) {
        log.info("用户信息", shoppingCart);
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("service_id", shoppingCart.getServiceId());
        wrapper.eq("user_id", shoppingCart.getUserId());
        boolean exists = shoppingCartService.exists(wrapper);
        if (exists) {
            return Result.error("已经加入购物车");
        }


        if (shoppingCartService.save(shoppingCart)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 更新数据
     *
     * @param shoppingCart 实例对象
     * @return 实例对象
     */
    @Operation(summary = "更新数据")
    @PutMapping("update")
    public Result<Boolean> update(@RequestBody ShoppingCart shoppingCart) {
        if (shoppingCartService.updateById(shoppingCart)) {
            return Result.success();
        }
        return Result.error();
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Operation(summary = "通过主键删除数据",
            parameters = {
                    @Parameter(name = "id", description = "主键", required = true, in = ParameterIn.PATH)
            })
    @DeleteMapping("/delete/{id}")
    public Result<Boolean> deleteById(@PathVariable String id) {

        if (shoppingCartService.removeById(id)) {
            return Result.success();
        }
        return Result.error();
    }

    @Operation(summary = "多id删除数据")
    @DeleteMapping("/delete/{ids}/{userid}")
    public Result<Boolean> deleteByIds(@PathVariable List<String> ids, @PathVariable String userid) {
        log.info("前端信息：{}", ids);
        int delete = shoppingCartMapper.delete(new QueryWrapper<ShoppingCart>().in("id", ids).eq("user_id", userid));
        if (delete > 0) {
            return Result.success();
        }
        return Result.error();
    }
}